#include <iostream>
#include <csignal>

#include <glog/logging.h>
#include <evpp/logging.h>

#include <evpp/duration.h>
#include <evpp/event_loop.h>
#include <evpp/buffer.h>
#include <evpp/tcp_callbacks.h>
#include <evpp/tcp_conn.h>
#include <evpp/tcp_server.h>
#include <evpp/invoke_timer.h>
#include <evpp/event_watcher.h>





#include "main_server.h"
#include "server_info.h"

using namespace evpp;

void PrintInfo() {
    std::cout << "client = " << ServerInfo::client_nums << std::endl;
}

int main(int argc, char const *argv[])
{
    google::InitGoogleLogging(argv[0]);
    google::SetLogDestination(google::GLOG_INFO, "/log/evpp_test");
    google::SetStderrLogging(google::GLOG_WARNING);

    LOG_INFO << "this is test";
    
    std::cout << "this is test" << std::endl;

    EventLoop* mainLoop = new EventLoop();

    MainServer* mainServer = new MainServer(mainLoop, "0.0.0.0:9999", 4);
    mainServer->SetDoneCallback([&mainLoop]() {
        mainLoop->Stop();
    });
    
    mainServer->Start();

    SignalEventWatcher signWatcher(SIGINT, mainLoop, [&mainLoop, &mainServer]() {
        std::cout << "Interrupt break" << std::endl;
        mainServer->Stop();
    });
    signWatcher.Init();
    signWatcher.AsyncWait();

    mainLoop->RunEvery(Duration(1.0), PrintInfo);

    mainLoop->Run();

    std::cout << "exiting..." << std::endl;

    delete mainServer;

    delete mainLoop;

    std::cout << "END." << std::endl;

    return 0;
}
